package com.example.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import com.example.common.enums.ResponseCodeEnum;
import com.example.common.utils.ApiResult;
import com.example.common.utils.CacheUtils;
import com.example.common.utils.HttpUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.cache.Cache;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.Map;

/**
 * person Controller
 * 2020-08-27 18:59
 */
@Controller
@RestController
@RequestMapping("/login")
@Slf4j
@CrossOrigin
@Api(value = "登录接口",tags={"登录接口"})
public class loginController {
    @Value("${idispatch_addr:}")
    private String idispatch_addr;

    /**
     * 缓存token
     */
    @GetMapping("/register")
    public ApiResult<Boolean> add(String token,String userName) {
        try {
            if(StringUtils.isBlank(token)){
                return new ApiResult<>(-9,"鉴权失败");
            }
            if(StringUtils.isBlank(userName)){
                return new ApiResult<>(-10,"鉴权失败");
            }
            //通过调度后台，校验token真实性
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("token",token);
            paramMap.put("operatorName",userName);
            log.info("调度地址"+idispatch_addr);
            String result = HttpUtils.postReturnString(idispatch_addr+"/tokendetection",paramMap);
            if(result.equals("fail")){
                return new ApiResult<>(-11,"鉴权失败");
            }
//            token = "Bearer " +token;
            CacheUtils.put(token, userName);
            log.info("登录成功"+token);
            return new ApiResult<>(0,"succ");
        }   catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(-11,"鉴权失败");
        }
    }

    /**
     * 查看缓存token
     */
    @GetMapping("/getCache")
    public ApiResult<Object> getCache() {
        try {
            Object obj = CacheUtils.get("1111");
            return new ApiResult<>(obj);
        }   catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.LOGIN_FALSE);
        }
    }

    /**
     * 查看缓存token
     */
    @GetMapping("/getCacheByKey/{key}")
    public ApiResult<Object> getCache(@PathVariable("key") String key) {
        try {
            Object obj = CacheUtils.get(key);
            return new ApiResult<>(obj);
        }   catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.LOGIN_FALSE);
        }
    }

    /**
     * 查看缓存token
     */
    @GetMapping("/getCacheBy")
    public ApiResult< Cache<String, Object>> getCacheBy() {
        try {
            Cache<String, Object> a = CacheUtils.getCache("sysCache");
            return new ApiResult<>(a);
        }   catch (Exception e) {
            e.printStackTrace();
            return new ApiResult<>(ResponseCodeEnum.LOGIN_FALSE);
        }
    }
}
